﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class RPTLoansPartOfCreditLines : System.Web.UI.Page
{
    bool mAutentificatedUser = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        mAutentificatedUser = Utils.AutentificatedUser;

        if (mAutentificatedUser)
        {
            loanPartOfCreditLineDateFromCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            loanPartOfCreditLineDateToCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            listOfPaymentInSelPerDateFromCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            listOfPaymentInSelPerDateToCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            grantedLoansFromCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            grantedLoansToCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            conEvalDateFromCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            conEvalDateToCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;
            parReportDateCalendarExtender.Format = Constants.Constants.ISODateBackwardDotsFormat;


            if (!IsPostBack)
            {
                if (Request["rpt"] != null && !Request["rpt"].ToString().Equals(string.Empty))
                {
                    string encodedString = Request["rpt"].ToString();

                    string reportName = Crypt.Module.DecodeCriptedString(Request["rpt"].ToString());

                    switch (reportName)
                    {
                        case Constants.Constants.ReportNames.ListOfPaymentsInSelectedPeriodReportName:
                            ShowPanel(ListOfPaymentsInSelectedPeriodPanel.ID);
                            break;

                        case Constants.Constants.ReportNames.LoanPartOfCreditsReportName:
                            ShowPanel(RPTLoansPartOfCreditLinesPanel.ID);
                            break;

                        case Constants.Constants.ReportNames.ClientPersonalDataReportName:
                            ShowPanel(personalDataReportPanel.ID);
                            break;

                        case Constants.Constants.ReportNames.ImprumuturiAcordateReportName:
                            ShowPanel(imprumuturiAcordatePanel.ID);
                            break;

                        case Constants.Constants.ReportNames.ConsultariEvaluariReportName:
                            ShowPanel(consultEvalReportPanel.ID);
                            break;

                        case Constants.Constants.ReportNames.ParReportName:
                            ShowPanel(parReportPanel.ID);
                            break;

                        default:
                            ShowPanel(emptyPanel.ID);
                            break;
                    }
                }
            }
        }
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        RPTLoansPartOfCreditLinesPanel.Visible = false;
        ListOfPaymentsInSelectedPeriodPanel.Visible = false;
        personalDataReportPanel.Visible = false;
        imprumuturiAcordatePanel.Visible = false;
        parReportPanel.Visible = false;

        #endregion Hide panels

        if (mAutentificatedUser)
        {
            try
            {
                #region Get Panel Name

                switch (panelName)
                {
                    case "RPTLoansPartOfCreditLinesPanel":
                        RPTLoansPartOfCreditLinesPanel.Visible = true;
                        {
                            DataTable sourceDataTable = Utils.ModuleCredits().GetFundingLinesList();
                            Utils.FillSelector(loanPartOfCreditLineCreditLineDLL, sourceDataTable, "Name", "LinesID");

                            DateTime beginYear = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                            loanPartOfCreditLineDateFromTextBox.Text = beginYear.ToString(Constants.Constants.ISODateBackwardDotsFormat);
                            loanPartOfCreditLineDateToTextBox.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);

                            FillLoanPartOfCreditLineReportGridView();
                        }
                        break;

                    case "ListOfPaymentsInSelectedPeriodPanel":
                        ListOfPaymentsInSelectedPeriodPanel.Visible = true;
                        {
                            DateTime beginYear = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                            listOfPaymentInSelPerDateFromTextBox.Text = beginYear.ToString(Constants.Constants.ISODateBackwardDotsFormat);
                            listOfPaymentInSelPerDateToTextBox.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);

                            DataTable programList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.Programs);
                            Utils.FillSelector(listOfPaymentInSelPerProgramDDL, programList, "Name", "Code");

                            DataTable creditLinesList = Utils.ModuleCredits().GetFundingLinesList();
                            Utils.FillSelector(listOfPaymentInSelPerFoundingLineDDL, creditLinesList, "Name", "LinesID");

                            DataTable loanStates = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CreditState);
                            Utils.FillSelector(listOfPaymentInSelPerLoanStateDDL, loanStates, "Name", "Code");
                            try
                            { listOfPaymentInSelPerLoanStateDDL.SelectedValue = ((int)Constants.Constants.Classifiers.CreditState_Activ).ToString(); }
                            catch { }


                            FillListOfPaymentInSelPerGridView();
                        }
                        break;

                    case "imprumuturiAcordatePanel":
                        imprumuturiAcordatePanel.Visible = true;
                        {
                            DateTime beginYear = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                            grantedLoansFromTextBox.Text = beginYear.ToString(Constants.Constants.ISODateBackwardDotsFormat);
                            grantedLoansToTextBox.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);

                            DataTable programList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.Programs);
                            Utils.FillSelector(grantedLoansProgramDDL, programList, "Name", "Code");

                            DataTable creditLinesList = Utils.ModuleCredits().GetFundingLinesList();
                            Utils.FillSelector(grantedLoansCreditLineDDL, creditLinesList, "Name", "LinesID");

                            FillImprumuturiAcordateGridView();
                        }
                        break;


                    case "consultEvalReportPanel":
                        consultEvalReportPanel.Visible = true;
                        {                     
                            DataTable operatorsLIst = Utils.ModuleCredits().GetOperatorsList();
                            Utils.FillSelector(conEvalOperatorDDL, operatorsLIst, "Operator Full Name", "OperatorID");

                            DataTable creditLinesList = Utils.ModuleCredits().GetFundingLinesList();
                            Utils.FillSelector(conEvalCreditLineDDL, creditLinesList, "Name", "LinesID");

                            DataTable programsList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.Programs);
                            Utils.FillSelector(conEvalProgramDDL, programsList, "Name", "Code");

                            DataTable tipConsultEval = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.TipEvaluareConsultare);
                            Utils.FillSelector(conEvalTipConsultDDL, tipConsultEval, "Name", "Code");
                            
                            DataTable stareaAfacerii = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.StareaAfacerii);
                            Utils.FillSelector(conEvalStateDDL, stareaAfacerii, "Name", "Code");

                            DateTime beginYear = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                            conEvalDateFromTextBox.Text = beginYear.ToString(Constants.Constants.ISODateBackwardDotsFormat);
                            conEvalDateToTextBox.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);

                            FillConsultEvalGridView();
                        }
                        break;

                    case "personalDataReportPanel":
                        personalDataReportPanel.Visible = true;
                        {
                            //DataTable countryList = Utils.GetMaster(this.Page).ServerObject.GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.CountryList);
                            //Utils.FillSelector(personalDataReport_CountryDDL, countryList, "Name", "Code");

                            //try
                            //{
                            //    personalDataReport_CountryDDL.SelectedValue = Utils.GetMaster(this.Page).ServerObject.DefaultCountry.ToString();
                            //}
                            //catch{}

                            //if (personalDataReport_CountryDDL.SelectedValue != null && !personalDataReport_CountryDDL.SelectedValue.ToString().Equals(string.Empty))
                            //{
                            //    int countruCOde = 0;
                            //    int.TryParse(personalDataReport_CountryDDL.SelectedValue.ToString(), out countruCOde);

                            //    DataTable b_provinceList = Utils.GetMaster(this.Page).ServerObject.GetProvinceListByCountry(countruCOde);
                            //    Utils.FillSelector(personalDataReport_RaionDDL, b_provinceList, "Name", "Code");
                            //}


                            DataTable b_provinceList = Utils.ModuleMain().GetProvinceListByCountry(Constants.Constants.DefaultCountry);
                            Utils.FillSelector(personalDataReport_RaionDDL, b_provinceList, "Name", "Code");

                            DataTable genderList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.GenderList);
                            Utils.FillSelector(personalDataReport_GenderDDL, genderList, "Name", "Code");

                            DataTable ciorchList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.Biserica);
                            Utils.FillSelector(personalDataReport_CiorchDDL, ciorchList, "Name", "Code");

                            DataTable sectorAfaceriList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.SectorAfaceri);
                            Utils.FillSelector(personalDataReport_SectorAfacereDDL, sectorAfaceriList, "Name", "Code");

                            DataTable programsList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.Programs);
                            Utils.FillSelector(personalDataReport_ProgramDDL, programsList, "Name", "Code");

                            DataTable creditLinesList = Utils.ModuleCredits().GetFundingLinesList();
                            if (creditLinesList != null)
                            {
                                creditLinesList.Rows.InsertAt(creditLinesList.NewRow(), 0);
                                creditLinesList.Rows[0]["Name"] = "**";
                                creditLinesList.Rows[0]["Code"] = 0;
                            }
                            Utils.FillSelector(crediLineDDL, creditLinesList, "Name", "LinesID");
                        }
                        break;

                    case "parReportPanel":
                        parReportPanel.Visible = true;
                        {
                            DataTable clientsDT = Utils.ModuleSecurity().UsersList();
                            Utils.FillSelector(managerCredtDDL, clientsDT, "UserFullName", "userid");

                            parReportDateTextBox.Text = DateTime.Now.ToString(Constants.Constants.ISODateBackwardDotsFormat);
                            FillParReportGridView();
                        }
                        break;   


                    default:
                        emptyPanel.Visible = true;
                        break;
                }

                #endregion Get Panel Name
            }
            catch (Exception ex)
            {
                Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
            }
        }
    }
    
    #region Loan Part of Credit Line

    private void FillLoanPartOfCreditLineReportGridView()
    {
        DateTime startDate = Crypt.Utils.ToDateTime(loanPartOfCreditLineDateFromTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);
        DateTime endDate = Crypt.Utils.ToDateTime(loanPartOfCreditLineDateToTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);

        int creditLineID = 0;
        int.TryParse(loanPartOfCreditLineCreditLineDLL.SelectedValue, out creditLineID);

        DataTable sourceTable = Utils.ModuleCredits().LoansPartOfCreditLine_Report(startDate, endDate, creditLineID);
       
        lansPartOfCreditLinesGridView.DataSource = sourceTable;
        lansPartOfCreditLinesGridView.DataBind();        
    }

    protected void loanPartOfCreditLineRefreshButton_Click(object sender, EventArgs e)
    {
        FillLoanPartOfCreditLineReportGridView();
    }

    protected void loanPartOfCreditLinePrintButton_Click(object sender, EventArgs e)
    {

    }

    #endregion Loan Part of Credit Line


    #region List Of Payments In Selected Period

    private void FillListOfPaymentInSelPerGridView()
    {
        DateTime startDate = Crypt.Utils.ToDateTime(listOfPaymentInSelPerDateFromTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);
        DateTime endDate = Crypt.Utils.ToDateTime(listOfPaymentInSelPerDateToTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);

        int programID = 0;
        int.TryParse(listOfPaymentInSelPerProgramDDL.SelectedValue, out programID);

        int foundingLineID = 0;
        int.TryParse(listOfPaymentInSelPerFoundingLineDDL.SelectedValue, out foundingLineID);
        bool filterByFoundingLine = listOfPaymentInSelPerFilterByFoundLineCheckBox.Checked;

        int loanStateID = 0;
        int.TryParse(listOfPaymentInSelPerLoanStateDDL.SelectedValue, out loanStateID);
        bool filterByLoanState = listOfPaymentInSelEnableLoanStateFilterCheckBox.Checked;

        bool filterByProgram = listOfPaymentInSelEnableProgramFilterCheckBox.Checked;


        DataTable sourceTable = Utils.ModuleCredits().ListOfPaymentsInSelectedPeriod_Report(startDate, endDate, foundingLineID, filterByFoundingLine, programID, filterByProgram, loanStateID, filterByLoanState);

        listOfPaymentInSelPerGridView.DataSource = sourceTable;
        listOfPaymentInSelPerGridView.DataBind();      
       
    }

    protected void listOfPaymentInSelPerRefreshButton_Click(object sender, EventArgs e)
    {
        FillListOfPaymentInSelPerGridView();
    }   

    protected void listOfPaymentInSelPerPrintButton_Click(object sender, EventArgs e)
    {

    }
    
    decimal sumPercentForFooter = 0;
    decimal sumMainRateForFooter = 0;
    decimal sumTotalAmountForFooter = 0;

    //decimal sumPercentForFooter_Rated = 0;
    //decimal sumMainRateForFooter_Rated = 0;
    //decimal sumTotalAmountForFooter_Rated = 0;

    protected void listOfPaymentInSelPerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label percentValueLabel = (Label)e.Row.FindControl("percentValueLabel");
            if (percentValueLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(percentValueLabel.Text, out tempVal);
                sumPercentForFooter += tempVal;
            }

            Label mainRateLabel = (Label)e.Row.FindControl("mainRateLabel");
            if (mainRateLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(mainRateLabel.Text, out tempVal);
                sumMainRateForFooter += tempVal;
            }

            Label totalAmountLabel = (Label)e.Row.FindControl("totalAmountLabel");
            if (totalAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(totalAmountLabel.Text, out tempVal);
                sumTotalAmountForFooter += tempVal;
            }            
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label totalPercentValueLabel = (Label)e.Row.FindControl("totalPercentValueLabel");
            totalPercentValueLabel.Text = sumPercentForFooter.ToString();

            Label totalMainRateLabel = (Label)e.Row.FindControl("totalMainRateLabel");
            totalMainRateLabel.Text = sumMainRateForFooter.ToString();

            Label totalTotalAmountLabelLabel = (Label)e.Row.FindControl("totalTotalAmountLabelLabel");
            totalTotalAmountLabelLabel.Text = sumTotalAmountForFooter.ToString();           
        }
    }

    protected void listOfPaymentInSelEnableProgramFilterCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        listOfPaymentInSelPerProgramDDL.Enabled = listOfPaymentInSelEnableProgramFilterCheckBox.Checked;
    }

    protected void listOfPaymentInSelPerFilterByFoundLineCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        listOfPaymentInSelPerFoundingLineDDL.Enabled = listOfPaymentInSelPerFilterByFoundLineCheckBox.Checked;
    }

    protected void listOfPaymentInSelEnableLoanStateFilterCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        listOfPaymentInSelPerLoanStateDDL.Enabled = listOfPaymentInSelEnableLoanStateFilterCheckBox.Checked;
    }

    #endregion List Of Payments In Selected Period

    
    #region Personal data Report 

    protected void personalDataRepor_RefreshButton_Click(object sender, EventArgs e)
    {
        FillpersonalDataReportGridView();
    }

    protected void FillpersonalDataReportGridView()
    {
        int countryCode = Constants.Constants.DefaultCountry;
        //if (personalDataReport_CountryDDL.SelectedValue != null && !personalDataReport_CountryDDL.SelectedValue.Equals(string.Empty))
        //    int.TryParse(personalDataReport_CountryDDL.SelectedValue, out countryCode);

        int raionCode = 0;
        if (personalDataReport_RaionDDL.SelectedValue != null && !personalDataReport_RaionDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(personalDataReport_RaionDDL.SelectedValue, out raionCode);

        string urmabRural = personalDataReport_UrbanRuralDDL.SelectedValue;

        int genderCode = 0;
        if (personalDataReport_GenderDDL.SelectedValue != null && !personalDataReport_GenderDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(personalDataReport_GenderDDL.SelectedValue, out genderCode);

        int churchCode = 0;
        if (personalDataReport_CiorchDDL.SelectedValue != null && !personalDataReport_CiorchDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(personalDataReport_CiorchDDL.SelectedValue, out churchCode);

        int sectorAfacereCode = 0;
        if (personalDataReport_CiorchDDL.SelectedValue != null && !personalDataReport_CiorchDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(personalDataReport_SectorAfacereDDL.SelectedValue, out sectorAfacereCode);

        int programID = 0;
        if (personalDataReport_ProgramDDL.SelectedValue != null && !personalDataReport_ProgramDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(personalDataReport_ProgramDDL.SelectedValue, out programID);

        int creditLineID = 0;
        if (crediLineDDL.SelectedValue != null && !crediLineDDL.SelectedValue.Equals(string.Empty))
            int.TryParse(crediLineDDL.SelectedValue, out creditLineID);


        DataTable sourceTable = Utils.ModuleCredits().ClientPersonalData_Report(countryCode, raionCode, urmabRural, genderCode, churchCode, sectorAfacereCode, programID, creditLineID);

        personalDataReportGridView.DataSource = sourceTable;
        personalDataReportGridView.DataBind();        
    }

    #endregion Personal data Report


    #region Imprumuturi acordate report
    private void FillImprumuturiAcordateGridView()
    {
        DateTime startDate = Crypt.Utils.ToDateTime(grantedLoansFromTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);
        DateTime endDate = Crypt.Utils.ToDateTime(grantedLoansToTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);

        int programID = 0;
        int.TryParse(grantedLoansProgramDDL.SelectedValue, out programID);
        bool filterByProgram = grantedLoansFilterByProgramCheckBox.Checked;

        int foundingLineID = 0;
        int.TryParse(grantedLoansCreditLineDDL.SelectedValue, out foundingLineID);
        bool filterByFoundingLine = grantedLoansFilterByCreditLineCheckBox.Checked;

        //bool showContractsOnlyInLey = grantedLoansOnlyInLeiRadioButton.Checked;


        DataTable sourceTable = Utils.ModuleCredits().ImprumuturiAcordateInSelectedPeriod_Report(startDate, endDate, foundingLineID, filterByFoundingLine, programID, filterByProgram);

        grantedLoansGridView.DataSource = sourceTable;
        grantedLoansGridView.DataBind();

        //if (showContractsOnlyInLey)
        //{
        //    grantedLoansGridView.Columns[5].Visible = false;
        //}
        //else
        //{
        //    grantedLoansGridView.Columns[5].Visible = true;
        //}
    }

    protected void grantedLoansRefreshButton_Click(object sender, EventArgs e)
    {
        FillImprumuturiAcordateGridView();
    }
    
    decimal gsumTotalAmountForFooter = 0;
    decimal gsumTotalAmountForFooter_Rated = 0;

    protected void grantedLoansGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //bool showContractsOnlyInLey = grantedLoansOnlyInLeiRadioButton.Checked;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label totalAmountLabel = (Label)e.Row.FindControl("creditAmountLabel");
            if (totalAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(totalAmountLabel.Text, out tempVal);
                gsumTotalAmountForFooter += tempVal;
            }

            //if (!showContractsOnlyInLey)
            //{
            //    Label TotalAmount_RatedLabel = (Label)e.Row.FindControl("creditAmount_RatedLabel");
            //    if (TotalAmount_RatedLabel != null)
            //    {
            //        decimal tempVal = 0;
            //        decimal.TryParse(TotalAmount_RatedLabel.Text, out tempVal);
            //        gsumTotalAmountForFooter_Rated += tempVal;
            //    }
            //}
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label totalTotalAmountLabelLabel = (Label)e.Row.FindControl("totalCreditAmountLabel");
            totalTotalAmountLabelLabel.Text = gsumTotalAmountForFooter.ToString();

            //if (!showContractsOnlyInLey)
            //{
            //    Label totalTotalAmount_RatedLabel = (Label)e.Row.FindControl("totalcreditAmount_RatedLabel");
            //    totalTotalAmount_RatedLabel.Text = gsumTotalAmountForFooter_Rated.ToString();
            //}
        }
    }

    protected void grantedLoansFilterByCreditLineCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        grantedLoansCreditLineDDL.Enabled = grantedLoansFilterByCreditLineCheckBox.Checked;
    }

    protected void grantedLoansFilterByProgramCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        grantedLoansProgramDDL.Enabled = grantedLoansFilterByProgramCheckBox.Checked;
    }

    #endregion Imprumuturi acordate report


    #region Consultari Evaluari 

    protected void FillConsultEvalGridView()
    {
        DateTime startDate = Crypt.Utils.ToDateTime(conEvalDateFromTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);
        DateTime endDate = Crypt.Utils.ToDateTime(conEvalDateToTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);

        int operatorID = 0;
        int.TryParse(conEvalCreditLineDDL.SelectedValue, out operatorID);
        bool filterByOperators = conEvalOperatorCheckBox.Checked;

        int foundingLineID = 0;
        int.TryParse(conEvalCreditLineDDL.SelectedValue, out foundingLineID);
        bool filterByFoundingLine = conEvalCreditLineCheckBox.Checked;

        int programID = 0;
        int.TryParse(conEvalProgramDDL.SelectedValue, out programID);
        bool filterByProgram = conEvalProgramCheckBox.Checked;

        int tipConsult = 0;
        int.TryParse(conEvalTipConsultDDL.SelectedValue, out tipConsult);
        bool filterByTipConsult = conEvalTipConsultCheckBox.Checked;

        int businessState = 0;
        int.TryParse(conEvalStateDDL.SelectedValue, out businessState);
        bool filterByBussinesState = conEvalStateCheckBox.Checked;

        DataTable sourceTable = Utils.ModuleCredits().ConsultEvals_Report(startDate, endDate, operatorID, filterByOperators, foundingLineID, filterByFoundingLine, programID, filterByProgram, tipConsult, filterByTipConsult, businessState, filterByBussinesState);
        conEvalGridView.DataSource = sourceTable;
        conEvalGridView.DataBind(); 
    }

    protected void conEvalOperatorCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        conEvalOperatorDDL.Enabled = conEvalOperatorCheckBox.Checked;
    }

    protected void conEvalCreditLineCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        conEvalCreditLineDDL.Enabled = conEvalCreditLineCheckBox.Checked;
    }

    protected void conEvalProgramCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        conEvalProgramDDL.Enabled = conEvalProgramCheckBox.Checked;
    }

    protected void conEvalTipCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        conEvalTipConsultDDL.Enabled = conEvalTipConsultCheckBox.Checked;
    }

    protected void conEvalStateCheckBox_CheckedChanged(object sender, EventArgs e)
    {
        conEvalStateDDL.Enabled = conEvalStateCheckBox.Checked;
    }

    protected void conEvalRefreshButton_Click(object sender, EventArgs e)
    {
        FillConsultEvalGridView();
    }    

    #endregion Consultari Evaluari 

    #region PAR Report

    protected void FillParReportGridView()
    {
        DateTime toDate = Crypt.Utils.ToDateTime(parReportDateTextBox.Text, Constants.Constants.ISODateBackwardDotsFormat);

        int managerID = 0;
        int.TryParse(managerCredtDDL.SelectedValue, out managerID);

        int minDelayAmount =0;
        int.TryParse(parReportMinAmountTextBox.Text, out minDelayAmount);

        DataTable sourceTable = Utils.ModuleCredits().PAR_Report(managerID, toDate, minDelayAmount);
        parReportGridView.DataSource = sourceTable;
        parReportGridView.DataBind(); 
    }

    protected void parReportRefreshButton_Click(object sender, EventArgs e)
    {
        FillParReportGridView();
    }


    int nrCurentLoanInReport = 0;
    decimal loanTotalAmount = 0;
    decimal loanTotalRemainingAmount = 0;
    decimal totalContractsPerManager = 0;

    protected void parReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            nrCurentLoanInReport++;

            Label loanAmountLabel = (Label)e.Row.FindControl("loanAmountLabel");
            if (loanAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(loanAmountLabel.Text, out tempVal);
                loanTotalAmount += Math.Round(tempVal, 2);
            }

            Label lremainingAmountLabel = (Label)e.Row.FindControl("lremainingAmountLabel");
            if (lremainingAmountLabel != null)
            {
                decimal tempVal = 0;
                decimal.TryParse(lremainingAmountLabel.Text, out tempVal);
                loanTotalRemainingAmount += Math.Round(tempVal, 2);
            }

            SetAmounts();
        }  
    }

    protected void SetAmounts()
    {
        if (totalContractsPerManager == 0)
        {
            int managerID = 0;
            int.TryParse(managerCredtDDL.SelectedValue, out managerID);
            if (managerID != 0)
            {
                totalContractsPerManager = Utils.ModuleCredits().GetManagersContract(managerID);
            }

            Label nrTotalContracteLabel = (Label)parReportGridView.HeaderRow.FindControl("nrTotalContracteLabel");
            if (nrTotalContracteLabel != null)
            {
                nrTotalContracteLabel.Text = totalContractsPerManager.ToString();
            }
        }       

        Label managerLabel = (Label)parReportGridView.HeaderRow.FindControl("managerLabel");
        if (managerLabel != null)
        {
            managerLabel.Text = managerCredtDDL.SelectedItem.Text;
        }

        Label nrContracteInRaportLabel = (Label)parReportGridView.HeaderRow.FindControl("nrContracteInRaportLabel");
        if (nrContracteInRaportLabel != null)
        {
            nrContracteInRaportLabel.Text = nrCurentLoanInReport.ToString();
        }

        Label totalAmountLabel = (Label)parReportGridView.HeaderRow.FindControl("totalAmountLabel");
        if (totalAmountLabel != null)
        {
            totalAmountLabel.Text = loanTotalAmount.ToString();
        }

        Label totalRemainingAmountLabel = (Label)parReportGridView.HeaderRow.FindControl("totalRemainingAmountLabel");
        if (totalRemainingAmountLabel != null)
        {
            totalRemainingAmountLabel.Text = loanTotalRemainingAmount.ToString();
        }
    }


    #endregion PAR Report


}